package com.ygxsk.carhome.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ygxsk.carhome.R;
import com.ygxsk.carhome.response.PayTypeList;
import com.ygxsk.carhome.utils.CommUtils;

import java.util.ArrayList;

//import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptnions.withCrossFade;

public class PayTypeAdapter extends BaseAdapter {

    private Activity activity;

    private Context context;

    private LayoutInflater mLayoutInflater;

    /**
     * 原始传递过来的支付方式的数据类型
     */
    private ArrayList<PayTypeList> filesReplace = new ArrayList<PayTypeList>();

    /**
     * 服务器返回的支付类型
     */
    private String payTypeFromServer;

    public interface PayTypeListener {

        void payType(PayTypeList payTypeList);

        void setPayTypeList(PayTypeList payTypeList);

    }


    PayTypeListener payTypeListener = null;

    public void setPayTypeListener(PayTypeListener _payTypeListener) {
        this.payTypeListener = _payTypeListener;
    }

    private ArrayList<Boolean> isChekeds = new ArrayList<Boolean>();

    /**
     * @date 2018/1/26 0026
     * @auther itchenqi
     * @description _payTypeFromServer服务器返回的默认类型/_filesReplace服务器返回的列表内容。
     */
    public PayTypeAdapter(Activity _activity,//
                          Context _context,//
                          ArrayList<PayTypeList> _filesReplace, //
                          String _payTypeFromServer) {//
        super();
        this.activity = _activity;
        this.context = _context;
        this.filesReplace = _filesReplace;
        this.payTypeFromServer = _payTypeFromServer;

        mLayoutInflater = CommUtils.getLayoutInflater(_context);

        if (isChekeds == null) {
            isChekeds = new ArrayList<Boolean>();
        }

        if (_payTypeFromServer != null && (!TextUtils.isEmpty(_payTypeFromServer))) {

            if ((_filesReplace != null) && (!_filesReplace.isEmpty())) {

                for (int i = 0; i < _filesReplace.size(); i++) {

                    //String payTypeCode = _filesReplace.get(i).getPayTypeCode();//支付类型
                    //int curFromServer = _payTypeFromServer.intValue();

                    if (TextUtils.equals(_filesReplace.get(i).getPayTypeCode(), _payTypeFromServer)) {

                        isChekeds.add(true);
                        //2018年04月03日发现该处。无效
                    /*if (payTypeListener != null) {
                        payTypeListener.setPayTypeList(_filesReplace.get(i));
                    }*/
                    } else {
                        isChekeds.add(false);
                    }
                }
            }


        } else {//如果没有返回默认的支付类型 全部设置为false

            if ((filesReplace != null) && (!filesReplace.isEmpty())) {

                for (int i = 0; i < filesReplace.size(); i++) {
                    isChekeds.add(false);
                }

            }

        }

        //这种待查看内容--2018年04月03日发现这一块是没有用的。
        /*for (int i = 0; i < isChekeds.size(); i++) {
            if (isChekeds.get(i)) {
				if (payTypeListener!=null) {
					payTypeListener.setPayTypeList(_filesReplace.get(i)); 
				}
			}
		}*/
    }

    @Override
    public int getCount() {
        return filesReplace == null ? 0 : filesReplace.size();
    }

    @Override
    public Object getItem(int position) {
        if (filesReplace != null && (position < getCount())) {
            return filesReplace.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.paytypeadapter, parent, false);
        }

        LinearLayout layoutView = SparseArrayViewHolder.get(convertView, R.id.layoutcontentciewnomal);
        ImageView imageViewPay = SparseArrayViewHolder.get(convertView, R.id.imageviewpaynomal);//图片
        TextView textViewPay = SparseArrayViewHolder.get(convertView, R.id.textviewpaynomal);//文字
        ImageView imageButtonPay = SparseArrayViewHolder.get(convertView, R.id.imagebuttonpaynomal);//单选按钮
        TextView textViewLinePayType = SparseArrayViewHolder.get(convertView, R.id.textviewlinepaytype);//最底部的帮助线条

        PayTypeList payTypeList = filesReplace.get(position);

        String imageUrl = payTypeList.getImage();
        String text = payTypeList.getPayName();
        //int type = payTypeList.getType();

        //添加支付方式图标
        Glide.with(context)//
                .load(imageUrl.trim())//
                .placeholder(R.drawable.ic_marker_inactive)//
                .error(R.drawable.ic_marker_inactive)//
                .fallback(R.drawable.ic_marker_inactive)//
                .into(imageViewPay);//
        //.transition(withCrossFade())

        CommUtils.setText(textViewPay, text);

        if (isChekeds.get(position)) {//如果被设定为true则展示
            if (payTypeListener != null) {
                payTypeListener.setPayTypeList(payTypeList);
            }
            CommUtils.setViewVisible(imageButtonPay);
        } else {
            CommUtils.setViewGone(imageButtonPay);
        }

        //2018年01月26日 需要重新替换对勾图片。
        //2018年03月23日 发现该处代码无效
        /*if (imageButtonPay.getVisibility() == View.VISIBLE) {
            if (payTypeListener != null) {
                payTypeListener.setPayTypeList(payTypeList);
            }
        }else{
            //retain
        }*/

        if (layoutView != null) {
            layoutView.setOnClickListener(new RadioClicker(position, payTypeList));
        }

        //2019年06月19日提交 去掉最后一个线条，留着吧，不留着 挺难看
        /*if ((filesReplace.size() - 1) == position){
            textViewLinePayType.setVisibility(View.GONE);
        }else{
            textViewLinePayType.setVisibility(View.VISIBLE);
        }*/

        return convertView;
    }

    private final class RadioClicker implements OnClickListener {

        private int position;
        private PayTypeList payTypeList;

        public RadioClicker(int _position, PayTypeList payTypeList) {
            super();
            this.position = _position;
            this.payTypeList = payTypeList;
        }

        @Override
        public void onClick(View v) {

            //click
            for (int i = 0; i < isChekeds.size(); i++) {

                if (i == position) {

                    isChekeds.set(position, true);

                    if (payTypeListener != null) {
                        payTypeListener.payType(payTypeList);
                    }

                } else {
                    isChekeds.set(i, false);
                }

            }

            PayTypeAdapter.this.notifyDataSetChanged();

        }

    }

}
